package logic

import (
	"context"
	"shop_srv/internal/svc"
	"shop_srv/model/mysql"
	"shop_srv/model/redis"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *shop.GoodsAddRequest) (*shop.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Goods{
		Title:  in.Title,
		Price:  float64(in.Price),
		Cover:  in.Cover,
		Stock:  in.Stock,
		Status: in.Status,
		TypeId: in.TypeId,
		UserId: in.UserId,
	}
	err := g.Create()
	if err != nil {
		return nil, err
	}
	redis.GoodsStocks(int64(g.ID), in.Stock)
	return &shop.GoodsAddResponse{GoodsId: int64(g.ID)}, nil
}
